home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / INGZP26A.TAR / internet / INGZP26A / CtrlPanel.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  10.0 KB  |  369 lines

  1. /* $Id: CtrlPanel.java,v 1.4 1996/03/31 11:43:02 djun Exp djun $
  2.  
  3.    File: CtrlPanel.java
  4.  
  5.    Author: Djun M. Kim
  6.    Copyright (c) 1996 Djun M. Kim.  All rights reserved.
  7.  
  8.    Notes:
  9.  
  10.    Things to do:
  11.  
  12.       *    Add the ability to work with and display multiple selections in 
  13.    the list.
  14.  
  15.       *    Internationalization support - labels and text displayed via
  16.    arrays of unicode strings indexed by language.
  17.  
  18. */
  19.  
  20. import java.awt.*;
  21. import java.net.*;
  22. import java.util.*;
  23.  
  24. public class CtrlPanel extends gridPanel {
  25.  
  26.     // our parent...
  27.     MapInfo parent;
  28.  
  29.     private int panelFunction;        // What is the MapPanel doing for the user?
  30.  
  31.     private static int numFuncs =     4;    // number of different fns
  32.     private String fnName[] =             // ... and their names.
  33.     {"Information", 
  34.     "Route Info",
  35.     "Detail maps",    
  36.     "Search"};
  37.  
  38.     // Constants
  39.     final int getLocationInfo =     0;    // Enumeration of function
  40.     final int getRouteInfo =         1;    // by symbolic name
  41.     final int getDetailInfo =         2;    
  42.     final int getSearchInfo =         3;    
  43.  
  44.     private CheckboxGroup fngrp = new CheckboxGroup();
  45.     public Checkbox cb_loc     = new Checkbox(fnName[getLocationInfo], fngrp, true);
  46.     public Checkbox cb_route     = new Checkbox(fnName[getRouteInfo],    fngrp, false);
  47.     public Checkbox cb_detail     = new Checkbox(fnName[getDetailInfo],    fngrp, false);
  48.     public Checkbox cb_search     = new Checkbox(fnName[getSearchInfo],     fngrp, false);
  49.  
  50.     final int REMN = GridBagConstraints.REMAINDER;
  51.  
  52.     Panel fnctn =     new Panel();        // panel for selecting function
  53.     Panel display =     new Panel();        // cardpanel for user function interaction
  54.  
  55.     CardLayout display_layout = new CardLayout();
  56.  
  57.     InfoPane      info_display;            // panels for various user functions
  58.     RoutePane     route_display; 
  59.     DetailPane    detail_display;
  60.     SearchPane    search_display;
  61.  
  62.     // Constructor
  63.  
  64.     CtrlPanel(MapInfo target) {
  65.     super(target);
  66.     c.insets = new Insets(5, 5, 0, 5);
  67.     // Get a hold of our parent...
  68.     this.parent = target;
  69.         display.setLayout(display_layout);
  70.      makeControls ();
  71.     }
  72.  
  73.     // Top-level control panel
  74.     void makeControls (){
  75.     //           Panel     gridx    gridy    Weightx Weighty GridW   GridH
  76.     make_panel(fnctn,      1,    1,    1.0,    0.0,    REMN,    1);
  77.     make_panel(display,      1,    2,    1.0,    1.0,    REMN,    REMN);
  78.     // add radio buttons
  79.     fnctn.setLayout(new GridLayout(3, 2));
  80.     fnctn.add(cb_loc);
  81.     fnctn.add(cb_route);
  82.     fnctn.add(cb_detail);
  83.     fnctn.add(cb_search);
  84.  
  85.     info_display =   new InfoPane("info_display", display, parent);
  86.     route_display =  new RoutePane(display, parent);
  87.     detail_display = new DetailPane(display, parent);
  88.     search_display = new SearchPane("search_display", display, parent);    
  89.     }
  90.  
  91.     // Event handler
  92.     public boolean action(Event evt, Object arg) {
  93.     if (evt.target instanceof Checkbox) {
  94.         Checkbox cb = (Checkbox)evt.target;
  95.         String cbox_seln = cb.getLabel();
  96.         if (cbox_seln == fnName[getLocationInfo]) {
  97.         panelFunction = getLocationInfo;
  98.          parent.setInfo("Show information about selected location");
  99.         display_layout.show(display, "info_display");
  100.         } else 
  101.         if (cbox_seln == fnName[getRouteInfo]) {
  102.         panelFunction = getRouteInfo;
  103.          parent.setInfo("Show route information");
  104.         display_layout.show(display, "route_display");
  105.         } else 
  106.         if (cbox_seln == fnName[getDetailInfo]) {
  107.         panelFunction = getDetailInfo;
  108.          parent.setInfo("Show detailed map");
  109.         display_layout.show(display, "detail_display");
  110.         } else 
  111.         if (cbox_seln == fnName[getSearchInfo]) {
  112.         panelFunction = getSearchInfo;
  113.          parent.setInfo("Search for location");
  114.         display_layout.show(display, "search_display");
  115.         } else   // default:    
  116.         {
  117.             panelFunction = getLocationInfo;
  118.          parent.setInfo("Show information about selected location");
  119.         display_layout.show(display, "info_display");
  120.         }
  121.         return true;        
  122.     } else
  123.         return false;
  124.     }    
  125.  
  126.     // returns the current user function of the Map panel
  127.     public int getPanelFunction(){
  128.     return panelFunction;
  129.     };
  130.  
  131.     // set the current user function of the Map panel
  132.     public void setPanelFunction(int function){
  133.     panelFunction = function;
  134.     switch (function) {
  135.         case getLocationInfo:
  136.         cb_loc.setState(true);
  137.         break;
  138.         case getRouteInfo:
  139.         cb_route.setState(true);
  140.         break;
  141.         case getDetailInfo:
  142.         cb_detail.setState(true);
  143.         break;
  144.         case getSearchInfo:
  145.         cb_search.setState(true);
  146.     };    
  147.     };
  148.  
  149.     void showCoords(int x, int y) {
  150.     parent.setInfo("X: " + x + ", Y: " + y);
  151.     }
  152. }
  153.  
  154. class InfoPane extends gridPanel {
  155.     // Information display panel
  156.  
  157.     MapInfo parent;
  158.  
  159.     TextField namefield;
  160.     TextField basefield;
  161.     TextField urlfield;
  162.     Button show_url;
  163.     TextArea infofield;
  164.     
  165.     InfoPane(String label, Container target, MapInfo parent) {
  166.     super(label, target);
  167.     this.parent = parent;    
  168.     //           Name        WeightX    WeightY    gridW    gridH
  169.     Label name = 
  170.        make_label("Name:",        0.0,    0.0,    1,     1); 
  171.     namefield = 
  172.         make_textf("Click on map to see name", false,
  173.                     0.6,    0.0,    REMN,     1);
  174.     Label base = 
  175.         make_label("Base Coords:",    0.0,    0.0,    1,     1); 
  176.     basefield = 
  177.         make_textf(" ", false,    0.6,    0.0,    REMN,     1);
  178.     show_url = 
  179.         make_button("Show URL",    0.0,    0.0,    1,     1); 
  180.     urlfield = 
  181.         make_textf(" ", false,    0.6,    0.0,    REMN,     1);
  182.     infofield = 
  183.         make_texta(" ", 8, 20,    1.0,    1.0,    REMN,     REMN);
  184.     }
  185.  
  186.     public void showLocationName(String s) {
  187.     namefield.setText(s);
  188.     }
  189.  
  190.     public void showLocationBase(Point p) {
  191.     if (p != null) {
  192.         basefield.setText("x:"+p.x+", y:"+p.y);
  193.     } else {
  194.             basefield.setText(" ");
  195.     }
  196.     }
  197.  
  198.     public void showLocationURL(URL url) {
  199.     if (url != null) {
  200.         urlfield.setText(url.toString());
  201.     } else {
  202.         urlfield.setText(" ");
  203.     }
  204.     }
  205.  
  206.     public void showLocationInfoText(String s) {
  207.     if (s != null) {
  208.         infofield.setText(s);
  209.     } else {
  210.         // Setting text to the empty string doesn't clear the old text!
  211.             infofield.setText(" ");
  212.     }
  213.     }
  214.  
  215.     // Clear the information panel.
  216.     public void clearLocationInfo(){
  217.     //parent.error_handler.debug("Clearing info.");
  218.     showLocationName(" ");
  219.     showLocationBase(null);
  220.     showLocationURL(null);
  221.     showLocationInfoText(" ");
  222.     }
  223.  
  224.     // Show information about the Location with base coordinates (x, y)
  225.     public void showLocationInfo(int x, int y) {
  226.     Location loc = parent.map.getLocation(x,y);
  227.     clearLocationInfo();
  228.     if (loc != null) {
  229.         showLocationName(loc.getName());
  230.         showLocationBase(loc.getBase());
  231.         showLocationURL(loc.getURL());
  232.         showLocationInfoText(loc.getInfoText());
  233.     }
  234.     }
  235.  
  236.     // Event handler
  237.     public boolean action(Event evt, Object arg) {
  238.     if (evt.target instanceof Button) {
  239.         if (((Button)evt.target) == show_url) {
  240.         Location loc = (Location)parent.mapview.canvas.getSelectedLocation();
  241.         if (loc != null) {
  242.             gotoURL(loc.getURL());
  243.         }
  244.         }
  245.         return true;
  246.     } else
  247.     return false;
  248.     }
  249.  
  250.     public void gotoURL(URL url) {
  251.     if (url != null)
  252.         parent.getAppletContext().showDocument(url);
  253.     }
  254. }
  255.  
  256.  
  257. class SearchPane extends gridPanel {
  258.     // Location search display panel
  259.  
  260.     MapInfo parent;
  261.  
  262.     Label searchfor;
  263.     TextField search_entry;
  264.     List locn_list;
  265.     Button show_info;
  266.  
  267.     SearchPane(String label, Panel target, MapInfo parent) {
  268.     super(label, target);
  269.     this.parent = parent;
  270.  
  271.     //           Name        WeightX    WeightY    gridW    gridH
  272.     Label searchfor = 
  273.        make_label("Search For:",    0.0,    0.0,    1,     1    ); 
  274.     search_entry = 
  275.         make_textf("Location", true,    
  276.                     1.0,    0.0,    REMN,     1    );
  277.     locn_list = 
  278.         make_list(" ", 7, false,    1.0,    1.0,    REMN,     1    );
  279.     show_info = 
  280.         make_button(" Show selection ",
  281.                     1.0,    0.0,    REMN,     1    ); 
  282.     }
  283.  
  284.     // Event handler
  285.     public boolean action(Event evt, Object arg) {
  286.  
  287.     SearchPane s = parent.ctrlview.search_display;
  288.     InfoPane info = parent.ctrlview.info_display;
  289.  
  290.     if (evt.target instanceof TextField) {
  291.         TextField tf = (TextField)evt.target;
  292.         if (tf == search_entry) {
  293.         searchEntry(tf.getText());
  294.         }
  295.         return true;
  296.     } else if (evt.target instanceof List) {
  297.         List list = (List)evt.target;
  298.         if (list.getSelectedItem() != null) {
  299.         // get the location
  300.         Location loc = parent.map.getLocationByName(list.getSelectedItem());
  301.         // select the location on the Map
  302.         parent.mapview.canvas.setSelectedLocation(loc);
  303.         parent.mapview.canvas.repaint();
  304.         // set a the button to offer to display more info...
  305.         Button b  = parent.ctrlview.search_display.show_info;
  306.         b.setLabel("Show info for "+list.getSelectedItem());
  307.         }
  308.         return true;
  309.     } else if (evt.target instanceof Button) {
  310.         Button b  = (Button)evt.target;
  311. //        b.setLabel("Show info for "+s.locn_list.getSelectedItem());
  312.         if (s.locn_list.getSelectedItem() != null) {
  313.         if (b == parent.ctrlview.search_display.show_info) {
  314.             Location loc = parent.map.getLocationByName(s.locn_list.getSelectedItem());
  315.             //parent.error_handler.debug("Selected location = "+loc.getName());
  316.             //parent.error_handler.debug("X:"+loc.getBase().x+", Y:"+loc.getBase().y);
  317.             parent.ctrlview.display_layout.show(
  318.             (Container)parent.ctrlview.display,"info_display");
  319.             parent.ctrlview.setPanelFunction(parent.ctrlview.getLocationInfo);
  320.             info.showLocationInfo(loc.getBase().x, loc.getBase().y);
  321.         }
  322.         }
  323.         return true;
  324.     } else
  325.         return false;
  326.     }
  327.  
  328.     // Handle searching for locations 
  329.     public void searchEntry(String s) {
  330.     Vector matchingLocns;
  331.     parent.setInfo("Searching for locations matching "+s+"...");
  332.     matchingLocns = parent.map.findLocationsByKeyword(s);
  333.     locn_list.clear();
  334.     if (matchingLocns != null) {
  335.         int count = 0;
  336.         for (Enumeration e = matchingLocns.elements(); 
  337.             e.hasMoreElements(); ) {
  338.         Location loc  = (Location)e.nextElement();
  339.         locn_list.addItem(loc.getName());
  340.         count++;
  341.         }
  342.         parent.setInfo("Found "+count+" locations matching "+s+".");
  343.     } else {
  344.         parent.setInfo("No locations matched "+s+".");
  345.     }
  346.     }
  347. }
  348.  
  349. // Route display panel
  350. class RoutePane extends Panel {
  351.  
  352.     RoutePane(Panel target, MapInfo parent) {
  353.     target.add("route_display", this);
  354.     setLayout(new GridLayout(1,1));
  355.     add(new Button("Routes"));
  356.     }
  357. }
  358.  
  359. // Detail display panel
  360. class DetailPane extends Panel {
  361.  
  362.     DetailPane(Panel target, MapInfo parent) {
  363.     target.add("detail_display", this);
  364.     setLayout(new GridLayout(1,1));
  365.     add(new Button("Show location details"));
  366.     }
  367. }
  368.  
  369.